<template>
  <div class="app-container">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="图层名称" prop="layer_name">
          <el-input
            v-model="queryParams.layer_name"
            placeholder="请输入图层名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="layerList" border :height="getAvailableHeight">
        <el-table-column type="index" width="40" align="center" label="#" :fixed="true"/>
        <el-table-column label="表名称" align="center" prop="target_table" width="150"/>
        <el-table-column label="服务地址" align="left" prop="service_url"/>
        <el-table-column label="图层前缀" align="center" prop="layer_prefix" width="100"/>
        <el-table-column label="图层名称" align="center" prop="layer_name" width="160"/>
        <el-table-column label="图层类型" align="center" prop="layer_type" width="120">
          <template slot-scope="scope">
            <span>{{ ['管点', '管线', '分区计量'][parseInt(scope.row.layer_type) - 1] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="坐标系" align="center" prop="srid" width="120">
          <template slot-scope="scope">
            <span>{{ 'EPSG:' + scope.row.srid }}</span>
          </template>
        </el-table-column>
        <el-table-column label="图层描述" align="left" prop="description"/>
        <el-table-column label="创建时间" align="center" prop="create_time" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.create_time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="110" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :total="total"
        :page-sizes="[15, 30, 45, 60]"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改管网图层对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="表名称" prop="target_table">
            <el-input v-model="form.target_table" placeholder="请输入表名称"/>
          </el-form-item>

          <el-form-item label="服务地址" prop="service_url">
            <el-input v-model="form.service_url" placeholder="请输入服务地址"/>
          </el-form-item>

          <el-form-item label="图层前缀" prop="layer_prefix">
            <el-input v-model="form.layer_prefix" placeholder="请输入图层前缀"/>
          </el-form-item>

          <el-form-item label="图层名称" prop="layer_name">
            <el-input v-model="form.layer_name" placeholder="请输入图层名称"/>
          </el-form-item>

          <el-form-item label="图层类型" prop="layer_type">
            <el-radio-group v-model="form.layer_type">
              <el-radio :label="1">管点</el-radio>
              <el-radio :label="2">管线</el-radio>
              <el-radio :label="3">分区计量</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="坐标系" prop="srid">
            <el-input-number :min="1" v-model="form.srid" placeholder="请输入坐标系"/>
          </el-form-item>

          <el-form-item label="图层描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入图层描述"/>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </ContentWrap>
  </div>
</template>

<script>
import {
  findTableData,
  addTableData,
  delTableData,
  updateTableData
} from "@/api/leakage/pgedit";

export default {
  name: "GisLayer",
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管网图层表格数据
      layerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        layer_name: null,
      },
      // 表单参数
      form: {
        id: '',
        target_table: '',
        service_url: '',
        layer_prefix: '',
        layer_name: '',
        layer_type: '',
        srid: null,
        description: '',
      },
      // 表单校验
      rules: {
        target_table: [
          {required: true, message: "请输入表名称", trigger: "blur"}
        ],
        layer_prefix: [
          {required: true, message: "请输入图层前缀", trigger: "blur"}
        ],
        layer_name: [
          {required: true, message: "请输入图层名称", trigger: "blur"}
        ],
        layer_type: [
          {required: true, message: "请选择图层类型", trigger: "change"}
        ],
        srid: [
          {required: true, message: "请输入坐标系", trigger: "change"}
        ],
      },
      selectedTerminalsObj: undefined,
      editingTargetId: undefined
    };
  },
  created() {
    this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 236
    },
  },
  methods: {
    /** 查询管网图层列表 */
    getList() {
      this.loading = true;
      const query = {...this.queryParams}
      query.tableName = 'sys_gis_layer'
      if (query.layer_name) {
        query.fields = ['layer_name']
        query.fieldValues = {layer_name: query.layer_name}
      }
      findTableData(query).then(response => {
        if (response.code === 200 && response.data && response.data.length) {
          this.layerList = response.data
          this.total = response.data[0].total
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: '',
        target_table: '',
        service_url: '',
        layer_prefix: '',
        layer_name: '',
        layer_type: '',
        srid: null,
        description: '',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加管网图层";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const ID = row.id || this.ids
      // getStrategyParams(ID).then(response => {
      this.form = row;
      this.open = true;
      this.title = "修改管网图层";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
          if (valid) {
            const formData = {}
            formData.tableType = 0
            formData.tableName = 'gis_layer'
            formData.newRow = JSON.parse(JSON.stringify(this.form))
            formData.id = this.form.id
            delete formData.newRow.id
            delete formData.newRow.total
            formData.fields = Object.keys(formData.newRow)
            if (formData.id != null && formData.id !== '') {
              updateTableData(formData).then(response => {
                if (response.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.open = false;
                  this.$modal.msgError(response.msg);
                }
              });
            } else {
              addTableData(formData).then(response => {
                if (response.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.open = false;
                  this.$modal.msgError(response.msg);
                }
              });
            }
          }
        }
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除所选管网图层？').then(function () {
        const formData = {}
        formData.tableName = 'gis_layer'
        formData.id = row.id
        return delTableData(formData);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
